package quiz;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class DisplayHistoryServlet
 */
@WebServlet("/DisplayHistory")
public class DisplayHistoryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DisplayHistoryServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Account acct = (Account) request.getSession().getAttribute("account");
		if (acct == null){
			request.setAttribute("error", "You're not logged in!  Please sign in to view your history.");
			RequestDispatcher dpatch = request.getRequestDispatcher("login.jsp");
			dpatch.forward(request, response);
			return;
		}
		
		DataBase db = (DataBase) request.getServletContext().getAttribute("db");
		ArrayList<Result> userHistory = db.getRecentResultsForUser(acct.getID(), Integer.MAX_VALUE);
		request.setAttribute("userHistory", userHistory);
		
		RequestDispatcher dpatch = request.getRequestDispatcher("user-history.jsp");
		dpatch.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
